<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="pink"></uni-icons>
		<button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
		<text class="login-text">登陆后尽享更多权益</text>
	</view>
</template>

<script>
	import {
		mapMutations,mapState
	} from 'vuex'
	export default {
		name: "my-login",
		data() {
			return {
			};
		},
		computed:{...mapState('m_user',['redireaInfo'])},
		methods: {
			...mapMutations('m_user', ['updateUserInfo','updateToken','updateRedireaInfo']),
			
			getUserInfo(e) {
				if (e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权')
				this.updateUserInfo(e.detail.userInfo)
				this.getToken(e.detail)
			},
			async getToken(info) {
				const res = await uni.login().catch(err => err)
				if (res.errMsg !== 'login:ok') return uni.$showError('登录失败')
				console.log(res.code)
				console.log(info)
				const query = {
					code: res.code,
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature,
				}
				this.updateToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
				this.navigateBack()
			},
		navigateBack(){
			if(this.redireaInfo&&this.redireaInfo.openType==='switchTab'){
				uni.switchTab({
					url:this.redireaInfo.from,
					complete:()=>{
						this.updateRedireaInfo(null)
					}
				})
			}
		}
		}
	}
</script>

<style lang="scss">
	.login-container {
		align-items: center;
		display: flex;
		flex-direction: column;
		justify-content: center;
		position: relative;
		text-align: center;
		height: 750px;

		&::after {
			content: ' ';
			display: block;
			position: absolute;
			width: 100%;
			height: 40px;
			left: 0;
			bottom: 0;
			background-color: white;
			border-radius: 100%;
			transform: translateY(50%);
		}

		button {
			width: 100%;
			margin: 15px 0;
			border-radius: 30px;
			background-color: #e68a7f;
		}

		.login-text {
			font-size: 12px;
			color: gray;
		}
	}
</style>